package com.example.controller;

import java.util.List;

import javax.transaction.Transactional;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Girl;
import com.example.domain.Result;
import com.example.repository.GirlRepository;
import com.example.service.GirlService;
import com.example.util.ResultUtil;

/**
 * 
 * @ClassName: GirlController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 强哥
 * @date 2017年12月8日 下午5:31:47
 *
 */
@RestController
public class GirlController {
	@Autowired
	private GirlRepository girlRepository;
	private final static Logger logger = LoggerFactory.getLogger(GirlController.class);

	/**
	 * 
	 * @Title: girlList @Description: TODO(获取所有数据) @author 强哥 @date 2017年12月11日
	 * 下午2:37:05 @param @return 设定文件 @return List<Girl> 返回类型 @throws
	 */
	@GetMapping(value = "/girls")
	public List<Girl> girlList() {
		// System.out.println("girlList");
		logger.info("girlList");
		return girlRepository.findAll();
	}

	/**
	 * 
	 * @Title: girlAdd @Description: TODO(新增一个数据) @author 强哥 @date 2017年12月11日
	 * 下午2:40:53 @param @param cupSize @param @param age @param @return
	 * 设定文件 @return Girl 返回类型 @throws
	 */
	@PostMapping(value = "/girls")
	// public Girl girlAdd(@RequestParam("cupSize")String cupSize,
	// @RequestParam("age") Integer age){
	public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult) {
		Result result =null;
		if (bindingResult.hasErrors()) {
			System.out.println(bindingResult.getFieldError().getDefaultMessage());
//			// return bindingResult.getFieldError().getDefaultMessage();
//			result.setCode(1);
//			result.setMsg(bindingResult.getFieldError().getDefaultMessage());
			logger.error("girlAdd:{}",bindingResult.getFieldError().getDefaultMessage());
			result = ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		} else {
			// Girl girl = new Girl(cupSize,age);
//			result.setData(girlRepository.save(girl));
			result = ResultUtil.success(girlRepository.save(girl));
		}
		return result;
	}

	// 查询一个女生
	@GetMapping(value = "/girls/{id}")
	public Girl girlFindOne(@PathVariable("id") Integer id) {
		return girlRepository.findOne(id);
	}

	// 更新
	@PutMapping(value = "/girls/{id}")
	public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam(value = "cupSize") String cupSize,
			@RequestParam(value = "age", required = false) Integer age) {
		Girl girl = new Girl();
		girl.setId(id);
		girl.setCupSize(cupSize);
		girl.setAge(age);
		return girlRepository.save(girl);
	}

	// 删除
	@DeleteMapping(value = "/girls/{id}")
	public void girlDelete(@PathVariable("id") Integer id) {
		System.out.println(id);
		girlRepository.delete(id);
	}

	// 通过年龄查询女生列表
	@GetMapping("/girls/age/{age}")
	public List<Girl> girlListByAge(@PathVariable("age") Integer age) {
		return girlRepository.findByAge(age);
	}

	@GetMapping("/girls/count")
	public Integer girlCount() {
		return girlRepository.girlCount();
	}

	@Autowired
	private GirlService girlService;

	
	@PostMapping("/girls/two")
	public void girlTwo() {
		girlService.insertTwo();
	}
	
	@GetMapping("/girls/getAge/{id}")
	public void getAge(@PathVariable("id") Integer id) throws Exception{
		girlService.getAge(id);
	}
	
	
	
	
	
}
